package com.feidian.helper.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.feidian.helper.R;
import com.feidian.helper.bean.Table;
import com.feidian.helper.bean.User;
import com.feidian.helper.net.NetManager;

public class FeedBackActivity extends Activity {
	private TextView mTitle;
	private ImageView mBack;
	private EditText editText;
	private Button bn;
	private static final int FEED_BACK = 0X123;
	private static final int FEED_BACK_FAILURE = 0X222;
	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case FEED_BACK: {
				Toast.makeText(FeedBackActivity.this,"谢谢你给我们团队的反馈" , Toast.LENGTH_LONG).show();
				editText.setText("");
				finish();

			}
				break;
			case FEED_BACK_FAILURE: {
				Toast.makeText(FeedBackActivity.this,"反馈失败" , Toast.LENGTH_LONG).show();
			}
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feed_back);
		init();
	}

	private void init() {
		mTitle = (TextView) findViewById(R.id.title);
		mTitle.setText("反馈");
		editText = (EditText) findViewById(R.id.content);
		bn = (Button) findViewById(R.id.permit);
		mBack = (ImageView) findViewById(R.id.back);
		mBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		bn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (User.stuId == null) {
					Toast.makeText(FeedBackActivity.this, "用户未登录",
							Toast.LENGTH_LONG).show();
					return;
				}
				if (editText.getText().toString().equals("")) {
					Toast.makeText(FeedBackActivity.this, "内容为空,不能提交",
							Toast.LENGTH_LONG).show();
					return;
				}
				String url = "http://115.29.103.105/User/FeedBack";
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair(Table.stuId, User.stuId));
				params.add(new BasicNameValuePair(Table.context, editText
						.getText().toString()));
				FeedBackThread feedBackThread=new FeedBackThread(url, params);
				feedBackThread.start();
				/*String context = NetManager.getNetManager().getWebWithPost(url,
						params);
				Message msg = mHandler.obtainMessage();
				if (context.equals("1")) {
					msg.what = FEED_BACK;
				} else {
					msg.what = FEED_BACK_FAILURE;
				}
				mHandler.sendMessage(msg);*/

			}
		});
	}
	class FeedBackThread extends Thread {
		private String mUrl;
		private List<NameValuePair> mParams;

		public FeedBackThread(String url, List<NameValuePair> params) {
			// TODO Auto-generated constructor stub
			mUrl = url;
			mParams = params;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			String context = NetManager.getNetManager().getWebWithPost(mUrl,
					mParams);
			if (context != null) {

				Log.e("contenxt=", context);
			}

			else
				Log.e("context=", "null");
			Message msg = mHandler.obtainMessage();
			if (context.equals("true")) {
				msg.what = FEED_BACK;
			} else {
				msg.what = FEED_BACK_FAILURE;
			}
			mHandler.sendMessage(msg);
			super.run();
		}
	}
}
